//
//  ZKTextImgReplaceHandle.m
//  朱凯的工具箱
//
//  Created by pro on 16/3/29.
//  Copyright © 2016年 zhukai. All rights reserved.
//

#import "ZKTextImgReplaceHandle.h"
#import "ImgTextAttachment.h"
@implementation ZKTextImgReplaceHandle

+ (NSMutableAttributedString *)handleText:(NSString *)text withRegex:(NSString *)regex {
    
    if (text == nil) {
        return nil;
    }
    
    
    // [:f001}
    if (regex == nil) {
        regex = @"\\[:f\\d{3}\\}";
    }
    NSMutableArray *nameAry = [[NSMutableArray alloc] init];
    
    NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:regex options:0 error:nil];

    if (reg != nil) {
        NSArray<NSTextCheckingResult *> *matchs = [reg matchesInString:text options:0 range:NSMakeRange(0, text.length)];
        for (NSTextCheckingResult *reult in matchs) {
            NSRange resultRange = [reult rangeAtIndex:0];
            NSString *name = [text substringWithRange:resultRange];
            [nameAry addObject:name];
        }
    } else {
        return [[NSMutableAttributedString alloc] initWithString:text];
    }
    

    //创建 附件对象，把附件添加到 AttributedString中
    NSMutableString *mutableStr = [NSMutableString stringWithString:text];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:text];
    for (NSString *str in nameAry) {
        ImgTextAttachment *attachment = [[ImgTextAttachment alloc] init];
        NSString *name = [str substringWithRange:NSMakeRange(2, 4)];
        UIImage *img = [UIImage imageNamed:name];
        attachment.image = img;
        
        NSAttributedString *imageAttributedString = [NSAttributedString attributedStringWithAttachment:attachment];
        
        NSRange range = [mutableStr rangeOfString:str];
//        NSLog(@"%@", NSStringFromRange(range));
//        NSLog(@"%li", mutableStr.length);
        [mutableStr replaceCharactersInRange:range withString:@" "];
        [attrString replaceCharactersInRange:range withAttributedString:imageAttributedString];

    }
    
    return attrString;
}



+ (CGFloat)getImgTextHeightWithText:(NSString *)text regex:(NSString *)regex size:(CGSize)size fontSize:(CGFloat)fontSize {
    
    if (text == nil) {
        return 0;
    }

    CGFloat originWidth = [text getSizeOfString:fontSize].width;
    
    // [:f001}
    if (regex == nil) {
        regex = @"\\[:f\\d{3}\\}";
    }
    NSMutableArray *nameAry = [[NSMutableArray alloc] init];
    
    NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:regex options:0 error:nil];
    if (reg != nil) {
        NSArray<NSTextCheckingResult *> *matchs = [reg matchesInString:text options:0 range:NSMakeRange(0, text.length)];
        for (NSTextCheckingResult *reult in matchs) {
            NSRange resultRange = [reult rangeAtIndex:0];
            NSString *name = [text substringWithRange:resultRange];
            [nameAry addObject:name];
        }
    }
//        return (originWidth / size.width) * fontSize;
    
    if (nameAry.count == 0) {
        return [text getBoundingRectOfStringSize:size font:fontSize].size.height;
    }
  
    
    NSString *faceStr = [nameAry componentsJoinedByString:@""];
//    NSLog(@"===%@",faceStr);
    CGFloat faceWidth = [faceStr getSizeOfString:fontSize].width;
//    NSLog(@"%f====%f",faceWidth,originWidth);
//    CGFloat retHeight = ceilf((nameAry.count * 17+ originWidth - faceWidth) / size.width) * MAX(17, fontSize);
//    CGFloat retHeight = ceilf(nameAry.count * 17 / size.width) * 17 + ceilf((originWidth - 17 * nameAry.count) / size.width) * fontSize;
    CGFloat retHeight = ceilf((originWidth - faceWidth + nameAry.count * 20)/size.width) * 17 + 3 * ceilf(nameAry.count * 17/size.width );
//    CGFloat retHeight = ((originWidth - faceWidth)/size.width * fontSize + nameAry.count * 17 * 17 /size.width);
    if (fontSize > 15) {
        return retHeight + 10;
    }
    return retHeight ;

}

@end
